<template>
  <div>
    <account-card title="商品添加">
      <el-form ref="form" label-width="80px" style="width: 600px">
        <el-form-item label="商品名称">
          <el-input size="small" v-model="name"></el-input>
        </el-form-item>
        <el-form-item label="商品分类">
          <el-select size="small" v-model="type" placeholder="请选择商品分类">
            <el-option
              :label="item.cateName"
              v-for="item in categorys"
              :key="item.cateName"
              :value="item.cateName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品价格">
          <el-input-number
            v-model="price"
            :min="1"
            :max="999"
            label="描述文字"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="商品描述">
          <el-input type="textarea" v-model="goodsDesc"></el-input>
        </el-form-item>
        <el-form-item label="商品图片">
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1:5000/goods/goods_img_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="imgUrl" :src="imgUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button @click="addgoods" :disabled='dis' size="small" type="primary"
            >立即创建</el-button
          >
        </el-form-item>
      </el-form>
    </account-card>
  </div>
</template>

<script>
import { goodsadd, goodscategories } from "@/apis/goods.js";
import AccountCard from "../../../components/accountcard/AccountCard.vue";

export default {
  components: {
    AccountCard,
  },
  data() {
    return {
      dis:false,
      name: "",
      goodsDesc: "",
      price: 1,
      imgUrl: "",
      type: "",
      categorys: [],
      value: "",
    };
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imgUrl = URL.createObjectURL(file.raw);
      this.newimg = res.imgUrl;
    },
    addgoods() {
      this.dis=true
      setTimeout(() => {
        this.dis=false
      }, 2000);
      goodsadd({
        name: this.name,
        category: this.type,
        price: this.price,
        imgUrl: this.newimg,
        goodsDesc: this.goodsDesc,
      }).then((res) => {
        console.log(res);
        this.$message({
          message: "添加成功",
          type: "success",
        });
      });
    },
  },
  created() {
    goodscategories().then((res) => {
      // console.log(res);
      this.categorys = res.data.categories;
    });
  },
};
</script>

<style lang="less" scoped>
.avatar-uploader /deep/ .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}
.avatar {
  width: 150px;
  height: 150px;
  display: block;
}
</style>